package com.etp.core;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.math.MathUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.RandomUtil;
import com.etp.util.ProductInfoExtractor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
import org.apache.poi.xssf.usermodel.XSSFPicture;
import org.apache.poi.xssf.usermodel.XSSFShape;

import javax.swing.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class App {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (Exception ignored) {}
            ProductInfoExtractor extrator = new ProductInfoExtractor();
            /**
             * 初始化输出目录
             */
            extrator.setVisible(true);
        });
    }

    public static void main_bak(String[] args) throws Exception {

        FileInputStream fis = new FileInputStream("D:/allProduct.xlsx");
        Workbook workbook = WorkbookFactory.create(fis);

        Sheet sheet = workbook.getSheetAt(0); // 获取第一个工作表

        System.out.println("sheet.getFirstRowNum()：" + sheet.getFirstRowNum());
        int i = 1;
        for(Row row: sheet) {
            //System.out.println("-----------" + i);
            // System.out.println("row:" +);

            Cell cell = row.getCell(3);
            System.out.println(i + ":cell---------" + cell);

            i++;
        }

        System.out.println("总量为：" + i);

        //Drawing<?> drawingPatriarch = sheet.getDrawingPatriarch();

        XSSFDrawing xssfDrawing = (XSSFDrawing)sheet.getDrawingPatriarch();
        List<XSSFShape> shapeList = new ArrayList<>();
        shapeList = xssfDrawing.getShapes();


        for(XSSFShape shape: shapeList) {
            if(shape instanceof XSSFPicture) {
                XSSFPicture picture = (XSSFPicture)shape;
                XSSFClientAnchor anchor = (XSSFClientAnchor)picture.getAnchor();

                int coll = anchor.getCol1();     // 图片左上角所在的列
                int rowl = anchor.getRow1();     // 图片左上角所在的行

                byte[] pictureByteArr = picture.getPictureData().getData();

                savePicture(pictureByteArr,(rowl + 1) + "行" + (coll + 1) + "列-" + RandomUtil.randomNumbers(4));

                System.out.println();
            }
        }



    }


    public static void savePicture(byte[] imageData,String fileName) throws Exception {

        try (FileOutputStream fos = new FileOutputStream("d:/productImages/" + fileName + ".png")) {
            fos.write(imageData);
            System.out.println("Image saved successfully!");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    public static void savePicture(byte[] imageData) throws Exception {

        try (FileOutputStream fos = new FileOutputStream("d:/productImages/p_" + RandomUtil.randomNumbers(10) + ".png")) {
            fos.write(imageData);
            System.out.println("Image saved successfully!");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }



}
